package com.example.shopping.utils;
import android.content.Context;
import android.content.SharedPreferences;
import com.example.shopping.me.UserBean;
import java.util.Map;
/* 1.共享参数存储     2.文件存储      3.数据库存储*/
public class SaveUserUtils {
    private static UserBean.DataBean userinfo;
    public static UserBean.DataBean getUserinfo() {
        return userinfo;
    }
    public static void setUserinfo(UserBean.DataBean info) {
        if (info!=null) {
            SaveUserUtils.userinfo = info;
        }
    }
    //删除用户内存信息
    public static void delUserInfo(){
        userinfo = null;
    }
    //   获取存储的用户信息
    public static UserBean.DataBean getUserToObj(Context context){
        SharedPreferences pref = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        int id = pref.getInt("id", -1);
        if (id!=-1) {
            String phone = pref.getString("phone","");
            String password = pref.getString("password", "");
            String name = pref.getString("name", "");
            String address = pref.getString("address", "");
            String nickname = pref.getString("nickname", "");
            String question = pref.getString("question", "");
            String answer = pref.getString("answer", "");
            UserBean.DataBean bean = new UserBean.DataBean(id, phone, password, name, address, nickname, question, answer);
            setUserinfo(bean);
            return bean;
        }
        return null;
    }
    /* 保存用户信息到本地文件的方法*/
    public static void saveUserInfo(Context context,UserBean.DataBean bean){
        SharedPreferences pref = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt("id",bean.getId());
        editor.putString("phone",bean.getPhone());
        editor.putString("password",bean.getPassword());
        editor.putString("name",bean.getName());
        editor.putString("address",bean.getAddress());
        editor.putString("nickname",bean.getNickname());
        editor.putString("question",bean.getQuestion());
        editor.putString("answer",bean.getAnswer());
        editor.commit();
        setUserinfo(bean);
    }

    /* 删除个人信息*/
    public static void deleteUserInfo(Context context,UserBean.DataBean bean){
        SharedPreferences pref = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.remove("id");
        editor.remove("phone");
        editor.remove("password");
        editor.remove("name");
        editor.remove("address");
        editor.remove("nickname");
        editor.remove("question");
        editor.remove("answer");
        editor.commit();
    }
}